package com.lrkj.payfee.service.impl;

import java.util.List;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.core.domain.entity.OrgTree;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.payfee.domain.MeterKind;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.payfee.mapper.RefillMapper;
import com.lrkj.payfee.domain.Refill;
import com.lrkj.payfee.service.IRefillService;

/**
 * 充值类型Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-07-27
 */
@Service
public class RefillServiceImpl implements IRefillService
{
    @Autowired
    private RefillMapper refillMapper;

    @Autowired
    private IOrgTreeService orgTreeService;

    /**
     * 查询充值类型
     *
     * @param refillId 充值类型ID
     * @return 充值类型
     */
    @Override
    public Refill selectRefillById(String refillId)
    {
        return refillMapper.selectRefillById(refillId);
    }

    /**
     * 查询充值类型列表
     *
     * @param refill 充值类型
     * @return 充值类型
     */
    @Override
    public List<Refill> selectRefillList(Refill refill)
    {
        return refillMapper.selectRefillList(refill);
    }

    /**
     * 新增充值类型
     *
     * @param refill 充值类型
     * @return 结果
     */
    @Override
    public int insertRefill(Refill refill)
    {
        refill.setRefillId(IdUtils.simpleUUID());
        refill.setCreateBy(SecurityUtils.getUsername());
        refill.setCreateTime(DateUtils.getNowDate());
//        this.setUnitInfo(refill);
        return refillMapper.insertRefill(refill);
    }

    /**
     * 修改充值类型
     *
     * @param refill 充值类型
     * @return 结果
     */
    @Override
    public int updateRefill(Refill refill)
    {
//        this.setUnitInfo(refill);
        refill.setUpdateBy(SecurityUtils.getUsername());
        refill.setUpdateTime(DateUtils.getNowDate());
        return refillMapper.updateRefill(refill);
    }

    /**
     * 批量删除充值类型
     *
     * @param refillIds 需要删除的充值类型ID
     * @return 结果
     */
    @Override
    public int deleteRefillByIds(String[] refillIds)
    {
        orgTreeService.deleteOrgTreeByIds(refillIds);
        return refillMapper.deleteRefillByIds(refillIds);
    }

    /**
     * 删除充值类型信息
     *
     * @param refillId 充值类型ID
     * @return 结果
     */
    @Override
    public int deleteRefillById(String refillId)
    {
        orgTreeService.deleteOrgTreeById(refillId);
        return refillMapper.deleteRefillById(refillId);
    }



    private void setUnitInfo(Refill bean) {
        OrgTree parentBean = orgTreeService.selectOrgTreeById(bean.getDeptId());
        bean.setAncestors(parentBean.getAncestors() + "," + bean.getDeptId());
        orgTreeService.insertOrgTree(bean.getRefillId(), bean.getRefillName(), bean.getDeptId(), OrgConstants.refill, bean.getAncestors());
    }
}
